let user = require('./user');

export function  downloadFileGet(url, param) {
    //_this.layer.load();
    const formData = new FormData();
    Object.keys(param).forEach((key) => {
        if(param[key] && param[key]!=""){
            formData.append(key, param[key]);
        }
    });

    let xhr = new XMLHttpRequest();
    let _this=this;
    // 请求结束
    xhr.onloadend = e => {

        let status = e.target.status;

        _this.layer.closeAll();
        if(status == 200){
            _this.$(".export-data").css("display","none");
            _this.layer.closeAll();
            const a = document.createElement('a');
            a.download = "导出数据.xls";
            a.style.display = 'none';
            a.href = window.URL.createObjectURL(e.target.response);
            document.body.appendChild(a);
            a.click();
            a.remove();
            window.URL.revokeObjectURL(href);
        }else if(status == 510){
            var reader = new FileReader();
            reader.onload = function(event){
                _this.layer.alert(JSON.parse(reader.result).msg);
            };
            reader.readAsText(e.target.response);
        }
    };


    xhr.timeout = 0; // 设置超时时间,0表示永不超时
    xhr.open('POST', url, true);
    // 设置期望的返回数据类型 'json' 'text' 'document' ...
    xhr.responseType = 'blob';
    // 设置请求头
    xhr.setRequestHeader('token', user.default.getToken());

    // 发送请求
    xhr.send(formData);
}

